/*
 * 录像模块样式
 * 2018年3月26日13:11:50
 * xwx
 */

(function() {

	var offset = 0;
	var limit = 5;
	var isflag = true;
	var search = '';
	var total; //总数

	mui.init({
		pullRefresh: {
			container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
			up: {
				height: '50px',
				auto: false,
				contentrefresh: '正在加载.....',
				contentmore: '暂无更多数据',
				callback: pullUprefresh
			},
			down: {
				style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
				color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
				height: '50px', //可选,默认50px.下拉刷新控件的高度,
				range: '100px', //可选 默认100px,控件可下拉拖拽的范围
				offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
				auto: false, //可选,默认false.首次加载自动上拉刷新一次
				callback: pullrefresh //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
			}
		}

	});

	//	直播列表数据参数
	var meetingPagerobj = {
		'offset': offset,
		'limit': limit,
		'recommend': '',
		"search": search,
		'begin': '',
		'end': '',
		'status': '4'
	}

	var userinfo = app.getUserInfo();
	initData();
	var wait;
	var querytype = 0; //默认查询全部录像数据
	var first = false;
	var homelist; //vue对象

	mui.plusReady(function() {
		window.addEventListener("myfresh", function(e) {
			pullrefresh();
		});
	});

	//	初始化绑定事件
	mui('body').on('tap', '.mui-card-content a', function() {
		var userid = userinfo.id;
		var meetingid = this.getAttribute("data-meetingId");
		var meetingpwd = this.getAttribute("data-password");
		var meetingstatus = this.getAttribute("data-status");
		var btnArray = ['取消', '确认'];
		if(meetingstatus != 0) { //判断当前会议室是否公开
			mui.prompt('请输入会议室密码：', '会议未公开', '', function(e) {
				if(e.index == 1) {
					if(meetingpwd != e.value) {
						mui.toast('会议室密码错误', {
							icon: 2
						});
						return;
					}
					onPenView(meetingid, userid);
				}
			}, 'div');

		} else {
			onPenView(meetingid, userid);
		}

	});

	//	初始化首页数据
	function initData() {

		server.meeting.getHistory(meetingPagerobj, function(r) {
			total = r.total;
			if(!first) { //首次加载动态数据，动态渲染DOM
				listMeetingObj = {
					total: r.total,
					items: r.rows
				};
				getDataDom();
				first = true;
			} else { //非首次进入，只更新DOM数据，DOM结构不变
				setTimeout(function() {
					if(wait) {
						wait.close();
					}
					homelist.items = r.rows; //	重新渲染vue对象DOM时，只需要赋值给初始化VUE对象对应的字段即可
				}, 500)
			}
		}, function(r) {
			mui.toast('服务器错误,请稍后刷新重试');
		})
	};

	//	动态切换样式
	function switchClass(num) {
		var switchNums = document.getElementById("cameraid").getElementsByTagName('a');
		for(var i = 0; i < switchNums.length; i++) {
			document.getElementById("cameraid").getElementsByTagName('a')[i].setAttribute('class', '')
		}
		document.getElementById("cameraid").getElementsByTagName('a')[num].setAttribute('class', 'cameraid-active')
	}

	//	日期查询对象{当前时间、最近一个月日期、最近三个月日期、最近半年日期}
	var QueryTime = {

		//	最近半年日期
		get6MonthBefor: function() {
			date = new Date();
			year = date.getFullYear();
			month = date.getMonth();
			sdate = date.getDate();
			olddate = new Date(year, month - 6, sdate);
			var arry = (olddate.getFullYear() + '-' + (olddate.getMonth() + 1) + '-' + olddate.getDate());
			return arry;
		},

		//	最近三个月日期
		get3MonthBefor: function() {
			var resultDate, year, month, date, hms;
			var currDate = new Date();
			year = currDate.getFullYear();
			month = currDate.getMonth() + 1;
			date = currDate.getDate();
			//hms = currDate.getHours() + ':' + currDate.getMinutes() + ':' + (currDate.getSeconds() < 10 ? '0' + currDate.getSeconds() : currDate.getSeconds());
			switch(month) {
				case 1:
				case 2:
				case 3:
					month += 9;
					year--;
					break;
				default:
					month -= 3;
					break;
			}
			month = (month < 10) ? ('0' + month) : month;
			//resultDate = year + '-' + month + '-' + date + ' ' + hms;
			resultDate = year + '-' + month + '-' + date;
			return resultDate;
		},

		//	最近一个月日期
		getPreMonth: function(date) {    
			var arr = date.split('-');    
			var year = arr[0]; //获取当前日期的年份
			    
			var month = arr[1]; //获取当前日期的月份
			    
			var day = arr[2]; //获取当前日期的日
			    
			var days = new Date(year, month, 0);    
			days = days.getDate(); //获取当前日期中月的天数
			    
			var year2 = year;    
			var month2 = parseInt(month) - 1;    
			if(month2 == 0) {        
				year2 = parseInt(year2) - 1;        
				month2 = 12;    
			}    
			var day2 = day;    
			var days2 = new Date(year2, month2, 0);    
			days2 = days2.getDate();    
			if(day2 > days2) {        
				day2 = days2;    
			}    
			if(month2 < 10) {        
				month2 = '0' + month2;    
			}    
			var t2 = year2 + '-' + month2 + '-' + day2;    
			return t2;
		},

		//	当前系统日期
		getNowFormatDate: function() {
			var date = new Date();
			var seperator1 = "-";
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentdate = year + seperator1 + month + seperator1 + strDate;
			return currentdate;
		}

	}

	//	按时间条件查询
	function queryDate(type) {
		var nowDate = QueryTime.getNowFormatDate();
		meetingPagerobj.end = nowDate;
		if(type == 2) {
			var prevDate = QueryTime.getPreMonth(nowDate);
			meetingPagerobj.begin = prevDate;
		} else if(type == 3) {
			var get3Date = QueryTime.get3MonthBefor();
			meetingPagerobj.begin = get3Date;
		} else {
			var get6Date = QueryTime.get6MonthBefor();
			meetingPagerobj.begin = get6Date;
		}

		initData();
	}

	//	渲染DOM数据
	function getDataDom() {

		// 渲染直播列表数据DOM
		homelist = new Vue({
			el: "#mui-home-list",
			data: listMeetingObj,
			methods: {
				handel: function(num) {
					meetingPagerobj.search = $("#searchinput").val();
					if(num != undefined) {
						switchClass(num);
						querytype = num;
						typeQuery(querytype);
					} else {
						if(meetingPagerobj.search == '') {
							mui.toast('关键字搜索');
							return;
						} else {
							num = $("#cameraid a.cameraid-active").index()
						}
					}
					meetingPagerobj.limit = 5;
					wait = plus.nativeUI.showWaiting('数据加载中');
					//console.log(num);
					switchClass(num);
					querytype = num;
					typeQuery(querytype);
				}
			}
		});

	}

	//	根据类别进行查询详情
	function typeQuery(num) {
		switch(num) {
			case 0:
				meetingPagerobj.recommend = '';
				meetingPagerobj.begin = '';
				meetingPagerobj.end = '';
				meetingPagerobj.search = $("#searchinput").val();
				initData();
				//console.log('查询全部')
				break;
			case 1:
				meetingPagerobj.recommend = 'true';
				meetingPagerobj.search = $("#searchinput").val();
				initData();
				//console.log('查询推荐')
				break;
			case 2:
				meetingPagerobj.recommend = '';
				meetingPagerobj.search = $("#searchinput").val();
				queryDate(2);
				//console.log('查询近一个月')
				break;
			case 3:
				meetingPagerobj.recommend = '';
				meetingPagerobj.search = $("#searchinput").val();
				queryDate(3);
				//console.log('查询近三个月')
				break;
			default:
				meetingPagerobj.recommend = '';
				meetingPagerobj.search = $("#searchinput").val();
				queryDate(4);
				//console.log('查询近半年')
				break;
		}
	}

	//	打开会议详情
	function onPenView(mid, uid) {
		mui.openWindow({
			url: 'detail.html',
			id: 'detail',
			extras: {
				"meetingid": mid, //自定义扩展参数，可以用来处理页面间传值
				"userid": uid
			},
			createNew: true, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
			show: {
				autoShow: true, //页面loaded事件发生后自动显示，默认为true
				aniShow: 'slide-in-right', //页面显示动画，默认为”slide-in-right“；
				duration: 300 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
			},
			waiting: {
				autoShow: true, //自动显示等待框，默认为true
				title: '正在加载...' //等待对话框上显示的提示内容
			}
		})
	};

	//	下拉刷新功能
	function pullrefresh() {
		typeQuery(querytype);
		setTimeout(function() {
			mui('#refreshContainer').pullRefresh().endPulldown();
		}, 1000);
	}

	//	上拉分页功能
	function pullUprefresh() {
		if(isflag) { //初始化isflag 为true,有分页
			isflag = total > limit + 5 ? true : false; // 判断总条数 与当前页显示条数+5 
			limit += 5;

			meetingPagerobj.limit = limit;
			initData();

			setTimeout(function() {
				mui('#refreshContainer').pullRefresh().endPullupToRefresh();
			}, 1000);
			pullrefresh();

		} else {
			setTimeout(function() {
				mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
			}, 1000);
		}

	}
})();